﻿
using System.Windows.Media;
using Wulong.Phone.Dianping.ViewModels;
using Wulong.Phone.Utils;

namespace Wulong.Phone.Dianping {
    using System;
    using System.Collections.Generic;
    using System.Windows.Controls;
    using Microsoft.Phone.Controls;
    using Caliburn.Micro;

    public class AppBootstrapper : PhoneBootstrapper
    {
        PhoneContainer _container;

        static AppBootstrapper()
        {
            //for caliburn debug
            LogManager.GetLog = type => new DebugLogger(type);
        }

        protected override void Configure()
        {
            _container = new PhoneContainer(RootFrame);

            _container.RegisterPhoneServices();
            _container.PerRequest<MainViewModel>();
            _container.PerRequest<SplashViewModel>();
            _container.PerRequest<YourCityViewModel>();
            _container.PerRequest<MerchantsByChannelViewModel>();
            _container.PerRequest<GroupOnItemViewModel>();
            _container.PerRequest<PromotionItemViewModel>();
            _container.PerRequest<MerchantPivotViewModel>();
            _container.PerRequest<SearchViewModel>();
            _container.PerRequest<PromotionListViewModel>();
            _container.PerRequest<GroupOnListViewModel>();
            _container.PerRequest<ChannelDialogViewModel>();
            _container.PerRequest<AboutViewModel>();
            AddCustomConventions();
        }

        protected override PhoneApplicationFrame CreatePhoneApplicationFrame()
        {
            return new TransitionFrame
                       {
                           //doing so due the toolkit transition affecting performance
                           //See Matt Lacey's blog for detail
                           Background = new SolidColorBrush(Colors.Transparent)
                       };
        }

        protected override object GetInstance(Type service, string key)
        {
            return _container.GetInstance(service, key);
        }

        protected override IEnumerable<object> GetAllInstances(Type service)
        {
            return _container.GetAllInstances(service);
        }

        protected override void BuildUp(object instance)
        {
            _container.BuildUp(instance);
        }

        static void AddCustomConventions()
        {
            ConventionManager.AddElementConvention<Pivot>(Pivot.ItemsSourceProperty, "SelectedItem", "SelectionChanged").ApplyBinding =
                (viewModelType, path, property, element, convention) => {
                    if (ConventionManager
                        .GetElementConvention(typeof(ItemsControl))
                        .ApplyBinding(viewModelType, path, property, element, convention))
                    {
                        ConventionManager
                            .ConfigureSelectedItem(element, Pivot.SelectedItemProperty, viewModelType, path);
                        ConventionManager
                            .ApplyHeaderTemplate(element, Pivot.HeaderTemplateProperty, viewModelType);
                        return true;
                    }

                    return false;
                };

            ConventionManager.AddElementConvention<Panorama>(Panorama.ItemsSourceProperty, "SelectedItem", "SelectionChanged").ApplyBinding =
                (viewModelType, path, property, element, convention) => {
                    if (ConventionManager
                        .GetElementConvention(typeof(ItemsControl))
                        .ApplyBinding(viewModelType, path, property, element, convention))
                    {
                        ConventionManager
                            .ConfigureSelectedItem(element, Panorama.SelectedItemProperty, viewModelType, path);
                        ConventionManager
                            .ApplyHeaderTemplate(element, Panorama.HeaderTemplateProperty, viewModelType);
                        return true;
                    }

                    return false;
                };

            ConventionManager.AddElementConvention<HubTile>(
                HubTile.DataContextProperty, "DataContext", "Tap");
            ConventionManager.AddElementConvention<ToggleSwitch>(
                ToggleSwitch.IsCheckedProperty, "IsChecked", "Checked");
            ConventionManager.AddElementConvention<ListPicker>(
                ListPicker.ItemsSourceProperty, "SelectedItem", "SelectionChanged");
            ConventionManager.AddElementConvention<PhoneTextBox>(
                PhoneTextBox.TextProperty, "Text", "TextChanged");
            ConventionManager.AddElementConvention<AutoCompleteBox>(
                AutoCompleteBox.ItemsSourceProperty, "SelectedItem", "SelectionChanged");
        }
    }
}